/**
 *  提交订单模块
 */
define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";

	$(document).on("click",".btn-group .btn",function(){
		if(!$(this).parent().hasClass("open")){$(this).parent().addClass("open");}else{$(this).parent().removeClass("open")}
		$(document).on("click",function(event){
			var _target=$(event.target).parents();
			if(!_target.hasClass("btn-group")){
				  $(".btn-group").removeClass("open");				  
			}else{_target.siblings(".btn-group").removeClass("open");}	
		});
	});
	
	//添加地址显示、隐藏
	/*$(document).on("click",".ldh-myAdress-new button",function(){
		$(this).parent(".ldh-myAdress-new").siblings(".ldh-myAdress-edit").toggle();	
	});
	*/
	require("../../custom/base/component-core.js");
		
	require("../../custom/base/component-md5.js");
	
	var validate = require("../../custom/base/component-validate.js");
	
	var base = require("../../custom/base/component-base.js");
	
	base.init(base);
		
	var async = require("../../custom/base/component-async.js");
	
	var head=require("../../custom/head/module-head.js");
	
	head.init(head);
	
		
	//初始化函数
	exports.init=function(m){
		
		window.$ldh_order=m;
		
		base.setFromPage("subOrder");
		
		var userId=$("#session_userid").val();
		
		if(!validate.isNull(userId)){
			
			base.hasLoginShow(userId,$("#session_username").val(),$("#session_iconurl").val());
			
		}else{
			
			base.loginShow();
		}
	
		document.title="云便利商店-提交订单";
		
		var url=decodeURI(document.URL);
		
		if(url.indexOf("?")>0){
			
			var params=url.split("?")[1];
			
			params=base.decodeParams(params);
			
			exports.getOrderPayMentType(params.split("=")[1]);
			exports.getOrderGoods(params.split("=")[1]);
			
		}
		
		exports.getMyAddressList();
		
		
	};
	
	var deliveryAmount;
	//查询订单商品
	exports.getOrderGoods=function(cartId){
		
		var params = {
				cartId : cartId
		};
		
		
		
			eval(
					Wind.compile("async", function() {

						var data = $await(async.DDF_AsyncRequest("POST", ctx+"/shopweb/Order/getOrderGoods.do", params));
						
					
						
						if(!base.validateLoginStatus(data)){
							return;
						}
						
						
						if(data["code"]==1){
							
							var htmlStr="";
							
							var index = 0,totalPrice=0,totalMerNum=0; 
							
							if(data["data"].length==0){
								
								$("#btn_suborder").attr("disabled","disabled");
								
								$("#btn_suborder").css("background-color","#D3D3D3");
								
							}
							
							for(var i=0;i<data["data"].length;i++){

											
											htmlStr += '<tbody>';
											htmlStr += '<tr><td class="text-left ldh-order-storeinfo" colspan="4"><span>店铺：<b>'+data["data"][i]["shopName"]+'</b></span></td></tr>';
											for (var j = 0; j < data["data"][i]["goods"].length; j++) {
												htmlStr += '<tr>';
												htmlStr += '<td>';
												htmlStr += '<table>';
												htmlStr += '<colgroup >';
												htmlStr += '<col width="16%" />';
												htmlStr += '<col width="41%" />';
												htmlStr += '</colgroup>';
												htmlStr += '<tbody> ';
												htmlStr += '<tr>';
												//htmlStr += '<td class="text-center"><div class="ldh-orderpic"><a class="pic" href="javascript:void(0)" target="_blank"><img src="'+fileOpenUrl+data["data"][i]["goods"][j]["picdefault"]+'" alt="查看宝贝详情" /></a></div></td>';
												//htmlStr += '<td class="text-left"><a class="text" href="product.html" target="_blank">'+data["data"][i]["goods"][j]["merchandiseName"]+' </a></td> ';
												var picDefault=data["data"][i]["goods"][j]["picdefault"];
												
												
												if(validate.isNull(picDefault)){
													
													picDefault = ctx+"/v1/img/default_234_220.png";

												}else{
													
													picDefault=fileOpenUrl+picDefault;

												}
												htmlStr += '<td class="text-center"><div class="ldh-orderpic"><img src="'+picDefault+'" alt="查看宝贝详情" /></div></td>';
												htmlStr += '<td class="text-left">'+data["data"][i]["goods"][j]["merchandiseName"]+' </td> ';
												htmlStr += '</tr>';
												htmlStr += '</tbody>';
												htmlStr += '</table>';
												htmlStr += '</td>';
												htmlStr += '<td>¥'+data["data"][i]["goods"][j]["price"]+'</td>';
												htmlStr += '<td>'+data["data"][i]["goods"][j]["nums"]+'</td>';
												htmlStr += '<td>¥'+(Number(data["data"][i]["goods"][j]["price"])*parseInt(data["data"][i]["goods"][j]["nums"])).toFixed(2)+'</td>';
												htmlStr += '</tr>';
												exports.setAddOrderParams("orderitemslist["+index+"].cartId",data["data"][i]["goods"][j]["cartId"]);
												exports.setAddOrderParams("orderitemslist["+index+"].merchandiseId",data["data"][i]["goods"][j]["merchandiseId"]);
												exports.setAddOrderParams("orderitemslist["+index+"].price",data["data"][i]["goods"][j]["price"]);
												exports.setAddOrderParams("orderitemslist["+index+"].nums",data["data"][i]["goods"][j]["nums"]);
												exports.setAddOrderParams("orderitemslist["+index+"].singlemertotal",(parseFloat(data["data"][i]["goods"][j]["price"])*parseInt(data["data"][i]["goods"][j]["nums"])));
												totalMerNum=totalMerNum+parseInt(data["data"][i]["goods"][j]["nums"]);
												index++;
											}
											htmlStr += '<tr>';
											htmlStr += '<td  colspan="3" class="text-left ldh-gessage-bbs"><span>给卖家留言：</span><textarea class="textarea" cols="100" ></textarea></td>';
											htmlStr += '<td class="text-right ldh-myOrder-total"><span>总计：</span><span>¥'+Number(data["data"][i]["price"]).toFixed(2)+'</span></td>';
											htmlStr += '</tr>';
											htmlStr += '</tbody>';
											
											totalPrice=totalPrice+Number(data["data"][i]["price"]);
		                      
								
							}
							
							
							htmlStr += ' <tbody>';
							htmlStr += '<tr><td colspan="4" class="text-right ldh-closingpay"><input name="businessHours" type="hidden" value="'+data["data"][0]["businessHours"]+'"/><input name="shopAddress" type="hidden" value="'+data["data"][0]["shopAddress"]+'"/><div> 总共<strong class=" main_color">'+totalMerNum+'</strong>件，<em id="deliveryDiv" style="display:none;">配送费:<strong class="main_color">¥<em id="deliveryAmountEm">'+Number(data["data"][0]["deliveryAmount"]).toFixed(2)+'</em></strong>，</em> 应付金额：<strong class="main_color">¥<em id="totalPriceEm">'+totalPrice.toFixed(2)+'</em><em id="totalPriceEmSource" style="display:none;">'+totalPrice.toFixed(2)+'</em></strong></div></td></tr>';
							htmlStr += '</tbody>';
	                        
							var minAmount=Number(data["data"][0]["minAmount"]);
							
							deliveryAmount=data["data"][0]["deliveryAmount"];
							
							if(minAmount<=totalPrice)
							{
								deliveryAmount=0;
								
							}
							
							$("#orderGoodList thead").after(htmlStr);
							
							exports.getShopDeliveryType(data["data"][0]["shopId"]);

							

							//给商家留言
							$(".ldh-gessage-bbs .textarea").focus(function(){
								$(this).addClass("textarea-focus");	
							});
							$(".ldh-gessage-bbs .textarea").blur(function(){
								$(this).removeClass("textarea-focus");	
							});
							
 
						}else if(data["code"]=="-10001"){
							
							alert(data["message"]);
							
						}
						
						
						
		}))().start();
		
	};
	
	//设置送货时间
	exports.setShipTime=function(shipTime,name){
		
		this.shipTime=shipTime;
		
		$("#span_shipTime").html(name);
		
		$(".btn-group").removeClass("open");	
	};
	
	//获取送货时间
	exports.getShipTime=function(){
		
		return this.shipTime==null?"尽快送达":this.shipTime;
		
	};
	
	
	//查询支付方式
	exports.getOrderPayMentType=function(cartId){
		
		var params={
				cartIds:cartId
			};

		eval(Wind.compile("async", function() {

			var data= $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Order/getOrderPayMentType.do", params));

			if(data.code == "1"){
				
				var htmlStr="";

				for (var i = 0; i < data["list"].length; i++) {
					var chk = ""; 
					if(data["list"][i].PAYMENT_ID=="1" || data["list"].length==1){
						chk='checked="checked"';
					}
					htmlStr += '<span><input type="radio" '+chk+' name="payment" value="'+data["list"][i].PAYMENT_ID+'"/>'+data["list"][i].PAYMENT_NAME+'</span>';
				}
				$("#orderPaymentType").html(htmlStr);
			}else{
				
			}
		}))().start();
		
	};
	
	
	//查询商家配送方式
	exports.getShopDeliveryType=function(shopId){
		
		var params={
				shopId:shopId
			};

		eval(Wind.compile("async", function() {

			var data= $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Order/getShopDeliveryType.do", params));

			if(data.code == "1"){
				
				var htmlStr="";
				
				
				for (var i = 0; i < data["list"].length; i++) {
					
					if(data["list"][i].DELIVERYID=="1")
					{
						if(deliveryAmount>0){
							htmlStr += '<span  onclick="$ldh_order.setDelivery(1)"><input type="radio"  name="delivery" checked="checked" value="1"/><a class="click_span">'+data["list"][i].DELIVERYNAME+'(<font class="main_color">未达到起送金额,配送费'+Number(deliveryAmount).toFixed(2)+'元</font>)</a></span>';
							$("#deliveryDiv").show();
							$("#totalPriceEm").html((Number($("#totalPriceEmSource").text())+Number(deliveryAmount)).toFixed(2));
						}else{
							htmlStr += '<span  onclick="$ldh_order.setDelivery(1)"><input type="radio"  name="delivery" checked="checked" value="1" /><a class="click_span">'+data["list"][i].DELIVERYNAME+'</a></span>';
							$("#deliveryDiv").hide();
						}
					}else{
						
						htmlStr += '<span onclick="$ldh_order.setDelivery('+data["list"][i].DELIVERYID+')"><input type="radio"  name="delivery" value="'+data["list"][i].DELIVERYID+'"  /><a class="click_span">'+data["list"][i].DELIVERYNAME+'</a></span>';

					}
					
				}
				$("#orderDeliveryType").html(htmlStr);
				
				
			}else{
				
			}
		}))().start();
		
	};
	
	//设置配送方式
	exports.setDelivery=function(deliveryId){
		
		
		if(deliveryId=="2")
		{
			//自提
			$("input[name='delivery'][type='radio'][value='2']").attr("checked","checked");
			$("input[name='payment'][type='radio'][value='2']").attr("checked","checked");
			$("input[name='payment'][type='radio'][value='1']").parent().hide();
			$("#deliveryDiv").hide();
			$("#totalPriceEm").html(Number($("#totalPriceEmSource").text()).toFixed(2));
			
			$("#deliveryAddress").hide();
			$("#shopAddress").show();
			$("#shopAddress span").html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+$("input[name='shopAddress']").val());
			
			$("#deliveryTime").hide();
			$("#zitiTime").show();
			
			if(validate.isNull($("input[name='businessHours']").val())){
				
				$("#zitiTime span").html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;任意时间");
				exports.setShipTime("任意时间");
			}else{
				
				$("#zitiTime span").html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+$("input[name='businessHours']").val());
				exports.setShipTime($("input[name='businessHours']").val());
			}
			
			
			
			
		}else{
			//商家配送
			$("input[name='delivery'][type='radio'][value='1']").attr("checked","checked");
			$("input[name='payment'][type='radio'][value='1']").parent().show();
			$("#deliveryAddress").show();
			$("#shopAddress").hide();
			$("#deliveryTime").show();
			$("#zitiTime").hide();
			if(deliveryAmount>0){
				
				$("#deliveryDiv").show();
				$("#totalPriceEm").html((Number($("#totalPriceEmSource").text())+Number(deliveryAmount)).toFixed(2));
			}
			exports.setShipTime($("#span_shipTime").text());
			
			
		}
		
	}
	
	
	//查询收货地址列表
	exports.getMyAddressList=function(){
				
		var params={
				userId:$("#session_userid").val(),
				currentPage:1,
				pageSize:10
			};

		eval(Wind.compile("async", function() {

			var data= $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Address/addressList.do", params));

			if(data.code == "1"){
				
				var htmlStr="";
				
				for(var i=0;i<data["addressList"].length;i++){
					

						htmlStr += '<li>';
						if(data["addressList"][i]["def"]=="1"){

							htmlStr += '<input type="radio" name="address" checked="checked" value="'+data["addressList"][i]["addressId"]+'">';
							
						}else{

							htmlStr += '<input type="radio" name="address" value="'+data["addressList"][i]["addressId"]+'">';
						}
							
						htmlStr += '<span>'+data["addressList"][i]["shipName"]+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+data["addressList"][i]["memo"]+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+data["addressList"][i]["shipMobile"]+'</span>';
						htmlStr += '<span class="ldh-suborderTop-adressedit">';
						htmlStr += '<a href=javascript:$ldh_order.loadAddress("'+data["addressList"][i]["addressId"]+'")>编辑</a>';
						htmlStr += '<a href=javascript:$ldh_order.deleteAddress("'+data["addressList"][i]["def"]+'","'+data["addressList"][i]["addressId"]+'")>删除</a>';
						htmlStr += '</span>';
						htmlStr += '</li>';
                        
                        
				}
				
				if(validate.isNull(htmlStr)){
					
					htmlStr+="<font color=#e62f17>收货地址不能为空,请先添加收货地址</font>";
				}
				
				$("#myAddressList").html(htmlStr);
				
			
			}else{
				
			}
		}))().start();
		
	};
	//打开添加收货地址
	exports.openEditAddress=function(type){
		
		if(type=="1"){
			
			$(".ldh-myAdress-edit").show();	
			
			exports.findAllProvince();
			
		}else{
			

			if($(".ldh-myAdress-edit").is(":visible")){
				
				$(".ldh-myAdress-edit").hide();	
				
			}else{
				
				$(".ldh-myAdress-edit").show();	
				
				exports.findAllProvince();
			}
			
		}
		
		
		
	
		
	};
	
	//保存收货地址
	exports.saveAddress=function(type){
		
		var provinceId=exports.getProvinceId();
		var cityId=exports.getCityId();
		var regionId=exports.getRegionId();
		var ship_address=$("#order_ship_address").val();
		var ship_mobile=$("#order_ship_mobile").val();
		var shipName=$("#order_shipName").val();
		
		$("#div_address_tip").show();
		
		if(validate.isNull(shipName)){
		   $("#div_address_tip span").html("收货人不能为空");
		   return;
		}
		if(validate.isNull(provinceId)){
			$("#div_address_tip span").html("请选择省份");
		   return;
		}
		if(validate.isNull(cityId)){
			$("#div_address_tip span").html("请选择城市");
		   return;
		}
		if(validate.isNull(regionId)){
			$("#div_address_tip span").html("请选择区域");
		   return;
		}
		if(validate.isNull(ship_address)){
		   $("#div_address_tip span").html("请填写详细地址");
		   return;
		}
		if(!validate.isMobile(ship_mobile)){
			   $("#div_address_tip span").html("请填写正确的手机号码");
			   return;
			}
		
		var memo=$("#province_name").text()+" "+$("#city_name").text()+" "+$("#region_name").text()+" "+ship_address;
		
		var params={
				"userId":$("#session_userid").val(),
				"provinceId":provinceId,
				"cityId":cityId,
				"regionId":regionId,
				"memo":memo,
				"shipName":shipName,
				"shipMobile":ship_mobile,
				"street":ship_address,
				"def":"1"
			};
		
		if(type=="1"){
			params["addressId"]=exports.getAddressId();
		}

		$("#div_address_tip span").html("正在提交,请稍候...");

		eval(Wind.compile("async", function() {
			var data;
			if(type=="0"){
				
				data= $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Address/saveAddress.do", params));

			}else{
				
				data= $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Address/updateAddress.do", params));

			}
			
			$("#div_address_tip").show();
			 
			if(data.code == "1"){
				
				 $(".ldh-myAdress-edit").hide();
				 
				  if(type=="1"){
					  
					 $("#div_address_tip span").html("编辑成功");
					  
					 $(".ldh-myAdress-new button").html("添加新地址");
						
					 $("#btn_edit_address").attr("onclick","$ldh_order.saveAddress(0)");
					 
				  }else{
					  
					  $("#div_address_tip span").html("添加成功");
				  }
				  
					exports.setProvinceId("","请选择");
					exports.setCityId("","请选择");
					exports.setRegionId("","请选择");
					$("#order_ship_address").val("");
					$("#order_ship_mobile").val("");
					$("#order_shipName").val("");
					
				 exports.getMyAddressList();
				
			}else{
				
				
				 $("#div_address_tip span").html(data.message);
			}
			
			task("#div_address_tip").start();
			
			
		}))().start();
		
	};
	
	//删除收货地址
	exports.deleteAddress=function(def,addressId){
		
		/*if(def=="1")
		{
		  alert("不能删除默认收货地址");
		  return;	
		}*/
		
		var params={
			"addressId":addressId
		};
		
		eval(Wind.compile("async", function() {

			var data= $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Address/delAddress.do", params));
			
			if(data.code == "1"){
				
				$(".ldh-myAdress-edit").hide();	
				
				 $(".ldh-myAdress-new button").html("添加新地址");
					
				 $("#btn_edit_address").attr("onclick","$ldh_order.saveAddress(0)");
				
				 exports.getMyAddressList();
				
			}else{
				
				
			}
			
			
		}))().start();
	};
	
	//加载收货地址,用户编辑
	exports.loadAddress=function(addressId){
		
		exports.setAddressId(addressId);
		
		exports.openEditAddress(1);
		
		$(".ldh-myAdress-new button").html("编辑收货地址");
		
		$("#btn_edit_address").attr("onclick","$ldh_order.saveAddress(1)");
		
		$("#div_address_loading").show();
		
		$("#div_address_tip").hide();
		
		var params={
				"addressId":addressId
			};
			
			eval(Wind.compile("async", function() {

				var data= $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Address/loadAddress.do", params));
				
				if(data.code == "1"){
					
					exports.setProvinceId(data["address"]["provinceId"]);
					exports.setCityId(data["address"]["cityId"]);
					exports.setRegionId(data["address"]["regionId"]);
					$("#order_ship_address").val(data["address"]["street"]);
					$("#order_ship_mobile").val(data["address"]["shipMobile"]);
					$("#order_shipName").val(data["address"]["shipName"]);
					$("#province_name").html(data["address"]["provinceName"]);
					$("#city_name").html(data["address"]["cityName"]);
					$("#region_name").html(data["address"]["regionName"]);
					
					
				}else{
					
					
				}
				$("#div_address_loading").hide();
				
			}))().start();
		
	};
	
	/**
	 * 查询所有省份
	 */
	exports.findAllProvince=function(){
		
		eval(
				Wind.compile("async", function() {

					var data = $await(async.DDF_AsyncRequest("POST", ctx
							+ "/shopweb/SysParam/findAllProvince.do", ""));
					
					findAllProvinceCallBack(data);

				}))().start();
		
	};
	
	/**
	 * 查询所有省份_回调函数
	 */
	function findAllProvinceCallBack(data){
		
		var htmlStr="";
		
		for(var i=0;i<data.length;i++){
			
			htmlStr+='<li><a href=javascript:$ldh_order.setProvinceId("'+data[i].PROVINCE_ID+'","'+data[i].PROVINCE_NAME+'")>'+data[i].PROVINCE_NAME+'</a></li>';
		}
		
		$("#provinceList").html('<li><a href=javascript:$ldh_order.setProvinceId("","请选择")>请选择</a></li>'+htmlStr);
	}
	
	/**
	 * 根据省份查询城市
	 */
	function findCityByProvince(provinceId){
		
		var params={
			provinceId:provinceId
		};
		
		eval(
				Wind.compile("async", function() {

					var data = $await(async.DDF_AsyncRequest("POST", ctx
							+ "/shopweb/SysParam/findCityByProvince.do",params));
					
					findCityByProvinceCallBack(data);

				}))().start();
	}
	
	/**
	 * 根据省份查询城市
	 */
	function findCityByProvinceCallBack(data){
		
		var htmlStr="";
		
		for(var i=0;i<data.length;i++){
						
			htmlStr+='<li><a href=javascript:$ldh_order.setCityId("'+data[i].CITY_ID+'","'+data[i].CITY_NAME+'")>'+data[i].CITY_NAME+'</a></li>';
		}
		
		$("#cityList").html('<li><a href=javascript:$ldh_order.setCityId("","请选择")>请选择</a></li>'+htmlStr);
	}
	
	/**
	 * 根据城市查询区域
	 */
	function findRegionByCity(cityId){
		
		var params={
				cityId:cityId
			};
		
		eval(Wind.compile("async", function() {

					var data = $await(async.DDF_AsyncRequest("POST", ctx
							+ "/shopweb/SysParam/findRegionByCity.do",params));
					
					findRegionByCityCallBack(data);

				}))().start();
	}
	
	/**
	 * 根据城市查询区域_回调函数
	 */
	function findRegionByCityCallBack(data){
		
		var htmlStr="";
		
		for(var i=0;i<data.length;i++){
			
			htmlStr+='<li><a href=javascript:$ldh_order.setRegionId("'+data[i].REGION_ID+'","'+data[i].REGION_NAME+'")>'+data[i].REGION_NAME+'</a></li>';

		}
		
		$("#regionList").html('<li><a href=javascript:$ldh_order.setRegionId("","请选择")>请选择</a></li>'+htmlStr);
		
	}
	
	exports.setAddressId=function(addressId){
		
		this.addressId=addressId;
	
	};
	
	exports.getAddressId=function(){
		
		return this.addressId==null?"":this.addressId;
		
	};
	
	exports.setProvinceId=function(provinceId,provinceName){
		
		this.provinceId=provinceId;
		
		$("#province_name").html(provinceName);
		
		$("#provinceList").parent().removeClass("open");
		
		exports.setCityId("","请选择");
		
		exports.setRegionId("","请选择");
		
		if(provinceId=="")
			return;
		
		findCityByProvince(provinceId);
		
	};
	
	exports.getProvinceId=function(){
		
		return this.provinceId==null?"":this.provinceId;
		
	};
	
	exports.setCityId=function(cityId,cityName){
		
		this.cityId=cityId;
		
		$("#city_name").html(cityName);
		
		$("#cityList").parent().removeClass("open");
		
		exports.setRegionId("","请选择");
		
		if(cityId=="")
			return;
		
		findRegionByCity(cityId);
		
	};
	
	exports.getCityId=function(){
		
		return this.cityId==null?"":this.cityId;
		
	};
	
	
	exports.setRegionId=function(regionId,regionName){
		
		this.regionId=regionId;
		
		$("#region_name").html(regionName);
		
		$("#regionList").parent().removeClass("open");
		
		
		if(regionId=="")
			return;
		
	};
	
	exports.getRegionId=function(){
		
		return this.regionId==null?"":this.regionId;
		
	};
	//定时提示信息
	var tips_task = eval(Wind.compile("async", function (divId) {
		
	    $await(Wind.Async.sleep(2000));
	   
	    $(divId).hide();
	    
	    
	}));
	
	var task = eval(Wind.compile("async", function (divId) {
	    $await(tips_task(divId));
	}));
	
	var params={};
	//设置订单参数
	exports.setAddOrderParams=function(key,value){
		
		params[key]=value;
		
	};
	
	//获取订单参数
	exports.getAddOrderParams=function(){
		
		return params==null?"":params;
		
	};
	
	//提交订单
	exports.addOrder=function(obj){
		
		var shipTime=exports.getShipTime();
		
		var addressId=$("input[name='address']:checked").val();
		
		var paymentId=$("input[name='payment']:checked").val();
		
		var deliveryId=$("input[name='delivery']:checked").val();
				
		if(validate.isNull(shipTime)){
			$("#orderErrorMsg").html("请选择送货时间");
			return;
		}
		
		if(validate.isNull(paymentId)){
			$("#orderErrorMsg").html("请选择支付方式");
			return;
		}
		
		if(validate.isNull(addressId)){
			$("#orderErrorMsg").html("请选择收货地址");
			return;
		}
		
		//验证码
		var randomCodeEle = document.getElementById("randomCode");
		if(randomCodeEle!=null){
			var randomCodeVal = randomCodeEle.value;
			if(validate.isNull(randomCodeVal)){
				$("#orderErrorMsg").html("请输入验证码");
				return;
			}else{
				exports.setAddOrderParams("randomCode",randomCodeVal);
			}
		}
		
		
		exports.setAddOrderParams("paymentId",paymentId);
		
		exports.setAddOrderParams("addressId",addressId);
		
		exports.setAddOrderParams("deliveryId",deliveryId);
		
		
		
		exports.setAddOrderParams("shipTime",shipTime);
		
		for(var i=0;i<$("#orderGoodList .textarea").length;i++){
			
			 exports.setAddOrderParams("orderMessagelist["+i+"].content",$("#orderGoodList .textarea:eq("+i+")").val());
		}
		
		$("#orderErrorMsg").html("");//清空错误消息
		
		$(obj).html("正在提交...");
		
		$(obj).attr("disabled","disabled");
		
		$(obj).css("background-color","#D3D3D3");
	
	
		
		eval(Wind.compile("async", function() {

			var data = $await(async.DDF_AsyncRequest("POST", ctx
					+ "/shopweb/Order/addOrder.do",exports.getAddOrderParams()));
			
			if(data.code=="1"){
				
				
				if(data["paymentId"]=='1'){
					
					get2post(ctx+"/v1/pages/order/order_success.jsp");
				}
				if(data["paymentId"]=='2'){
					var orders = data["orderIds"][1];
					if(orders!=null){
						
						get2post(ctx+"/v1/pages/ucenter/ucenter.jsp");
						}else{
							get2post(ctx+"/v1/pages/order/pay.jsp?order="+data["orderIds"][0]+"&orderIds="+data["orderIds"]+"&totalMoney="+Number(data["totalMoney"]).toFixed(2));
							
						}
				}
			}else{//错误
				exports.refresh();//刷新验证码
				$(obj).html("提交订单");
				
				$(obj).removeAttr("disabled");
				
				$(obj).css("background-color","#ed5d3a");
				
				$("#orderErrorMsg").html(data.message);//显示错误消息
			}

		}))().start();
	
	};
	//获取随机数图片
	exports.refresh=function() {
		var obj=document.getElementById("imgCode");
	    obj.src = ctx+"/ImageServlet?" + Math.random();
	};

});

